<?php
/***************************************************************************
 * @Description: 对象模型接口
 * 
 * @FileName:         ModelController.php
 * @Author :          liyong5@staff.sina.com.cn
 * @CreatedTime:      Tue 29 May 2018 10:15:13 AM CST
 ************************************************************************/

namespace app\modules\core\controllers;

use Yii;
use app\modules\core\models\Model;
use app\modules\core\Field;
use yii\data\ActiveDataProvider;
use yii\data\Pagination;

class ModelController extends DefaultController
{
    public function actions() {
        return array_merge(parent::actions(), []);
    }

    public function actionList() {
        $query = Model::find()->where([]);

        $pagination = new Pagination([
            'defaultPageSize' => 20,
            'totalCount' => $query->count(),
        ]);

        $models = $query->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();
        
        $models = array_map(function ($m){
            return $m->fillFields();
        }, $models);
        
        return $this->render('list', ['models' => $models, 'pagination' => $pagination]);
    }

    public function actionInfo() {
        $request = Yii::$app->request;

        $model = Model::find()
                ->where(['id' => strval($request->get('id'))])
                ->one();

        $model->fillFields();

        return $this->render('content', ['model' => $model]);
    }

    public function actionCreate() {
        $request = Yii::$app->request;

        $model = new Model();

        if (! $model->load($request->post())) {
            return $this->render('edit', ['model' => $model]);
        }

        $model->refCount = 0;
        $model->refList = [];
        $model->fcount = count($model->fields);
        $model->id  = $model->genId();
        if ($model->validate()
                && $model->save()) {
            return $this->redirect(['/core/model/list']);
        } else {
            $model->fillFields();
            return $this->render('edit', ['model' => $model]);
        }
    }

    public function actionEdit() {
        $request = Yii::$app->request;

        if (! empty($modelId = $request->post('id'))) {
            $model = Model::findOne(['id' => $modelId]);
        }
        else if (! empty($modelId = $request->get('id'))){
            $model = Model::findOne(['id' => $modelId]);
            $model->fillFields();
            return $this->renderEdit($model);
        }
        else {
            return $this->redirect(['/core/model/list']);
        }

        if (! $model->load($request->post())) {
            return $this->renderEdit($model);
        }

        $model->fcount = count($model->fields);
        if ($model->validate() && $model->save()) {
            return $this->redirect(['/core/model/info', 'id' => $modelId]);
        } else {
            return $this->renderEdit($model);
        }
    }

    private function renderEdit($model) {
        return $this->render('edit', ['model' => $model]);
    }

    public function actionDelete() {
        $request = Yii::$app->request;

        $model = Model::findOne(['id' => strval($request->get('id'))]);

        if ($model->delete()) {
            return $this->redirect(['/core/model/list']);
        } else {
            return $this->asJson([0, 'succ']);
        }
    }
}
